package com.wenlong.dao.impl;

import com.wenlong.dao.BashDao;
import com.wenlong.dao.CommodityDao;
import com.wenlong.pojo.Commodity;

import java.util.List;

/**
 * @author 苏文龙
 * @version 1.0
 */
public class CommodityDaoImpl extends BashDao<Commodity> implements CommodityDao {

    @Override
    public int addCommodity(Commodity commodity) {
        String sql = "insert into iuser.t_commodity(`name` , `price` , `author` ,`sales` , `stock` , `img_path`)" +
                " values(?,?,?,?,?,?)";
        return update(sql, commodity.getName(), commodity.getPrice(),
                commodity.getAuthor(), commodity.getSales(),
                commodity.getStock(), commodity.getImgpath());
    }

    @Override
    public int updateCommodity(Commodity commodity) {
        String sql = "update iuser.t_commodity set `name`=? ," +
                " `price`=? , `author`=? ,`sales`=? , `stock`=? , `img_path`=? where id=?";
        return update(sql, commodity.getName(), commodity.getPrice(),
                commodity.getAuthor(), commodity.getSales(),
                commodity.getStock(), commodity.getImgpath(), commodity.getId());
    }

    @Override
    public int deleteCommodity(int id) {
        String sql = "delete from iuser.t_commodity where id=?";
        return update(sql, id);
    }

    @Override
    public Commodity queryCommodityById(Integer id) {
        String sql = "select `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` imgpath " +
                "from iuser.t_commodity where id=?";
        return queryForOne(sql, Commodity.class, id);
    }

    @Override
    public List<Commodity> queryAllCommodity() {
        String sql = "select * from iuser.t_commodity";
        return queryForMulti(sql, Commodity.class);
    }

    @Override
    public int queryForPageTotalCount() {
        String sql = "select count(*) from t_commodity";
        Number number = (Number) queryForScalar(sql);
        return number.intValue();
    }

    @Override
    public List<Commodity> queryForPageCommodity(int begin, int pageSize) {
        String sql = "select `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` imgpath " +
                "from iuser.t_commodity limit ?,?";
        return queryForMulti(sql, Commodity.class, begin, pageSize);
    }

    @Override
    public int queryForPageTotalCountByPrice(int min, int max) {
        String sql = "select count(*) from t_commodity where price between ? and ?";
        Number number = (Number) queryForScalar(sql, min, max);
        return number.intValue();
    }

    @Override
    public List<Commodity> queryForPageCommodityByPrice(int begin, int pageSize, int min, int max) {
        String sql = "select `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` imgpath " +
                "from iuser.t_commodity where price between ? and ? order by price limit ?,?";
        return queryForMulti(sql, Commodity.class, min, max, begin, pageSize);
    }
}
